from django.shortcuts import render, redirect
from app01 import models
from app01.utils.form import UserModelForm,MobileModelForm,MobileeditModelForm

# Create your views here.
def mobile_list(request):
    """靓号列表"""
    # for i in range(300):
    #   models.MobileInfo.objects.create(mobile="15990908978", price= 10, level= 1, status=1)
    data_dict = {}
    search_data = request.GET.get('q', "")
    if search_data:
        data_dict['mobile__contains'] = search_data


    ## 导入自己编写的类，方便分页的代码重复编写
    from app01.utils.pagination import Pagination

    queryset = models.MobileInfo.objects.filter(**data_dict).order_by('-level')
    page_object = Pagination(request,queryset)
    page_queryset = page_object.page_queryset

    page_string = page_object.html()
    context = {
        'queryset': page_queryset,   # 分完页的数据
        "search_data": search_data,
        "page_string": page_string   # 页码
    }
    return render(request, 'mobile_list.html', context)




    # 根据用户想要访问的页码，计算出起止位置
    #page = int(request.GET.get('page', 1))
    #page_size = 10  # 每页显示10条数据
    #start = (page - 1) * page_size
    #end = page * page_size
    # 数据的总条数
    #total_count = models.MobileInfo.objects.filter(**data_dict).order_by('-level').count()

    # 计算总共有多少页码
    #total_page_count, div = divmod(total_count, page_size)
    #if div:
    #    total_page_count += 1


    # # 计算出，当前页的前五页和后五页
    # plus = 5
    # if total_page_count <= 2 * plus + 1:
    #     # 数据库中的数据比较少 没有达到11个
    #     start_page = 1
    #     end_page = total_page_count
    # else:
    #     # 数据库中的数据比较多
    #     # 当当前页<5页时
    #     if page <= plus:
    #         start_page = 1
    #         end_page = 2 * plus + 1
    #     else:
    #         # 当前页 > 5时
    #         # 当前页+5》总页码时
    #         if (page + plus) > total_page_count:
    #             start_page = total_page_count - 2 * plus
    #             end_page = total_page_count
    #         else:
    #             start_page = page - plus
    #             end_page = page + plus

    # # 页码
    # page_str_list = []
    # # 首页
    # prev = '<li><a href="?page={}">首页</a></li>'.format(1)
    # page_str_list.append(prev)
    # # 上一页
    # if page > 1:
    #     prev = '<li><a href="?page={}">上一页</a></li>'.format(page - 1)
    # else:
    #     prev = '<li><a href="?page={}">上一页</a></li>'.format(1)
    # page_str_list.append(prev)
    # # 页面
    # for i in range(start_page, end_page + 1):
    #     if i == page:
    #         ele = '<li class="active"><a href="?page={}">{}</a></li>'.format(i, i)
    #     else:
    #         ele = '<li><a href="?page={}">{}</a></li>'.format(i, i)
    #     page_str_list.append(ele)
    # # 下一页
    # if page < total_page_count:
    #     prev = '<li><a href="?page={}">下一页</a></li>'.format(page + 1)
    # else:
    #     prev = '<li><a href="?page={}">下一页</a></li>'.format(end_page)
    # page_str_list.append(prev)
    #
    # # 尾页
    # prev = '<li><a href="?page={}">尾页</a></li>'.format(total_page_count)
    # page_str_list.append(prev)
    #
    # page_string = mark_safe("".join(page_str_list))

    # # 返回渲染的页面
    # return render(request, 'mobile_list.html',
    #               {'queryset': queryset, "search_data": search_data, "page_string": page_string})

def mobile_add(request):
    """添加靓号"""
    if request.method == "GET":
        form = MobileModelForm()
        return render(request, 'mobile_add.html', {'form':form})
    # 用户post提交数据 数据集检验
    form = MobileModelForm(data=request.POST)
    if form.is_valid():
        # print(form.cleaned_data)
        # 如果数据合法，保存到数据库
        # models.UserInfo.objects.create()
        form.save()
        return redirect("/mobile/list/")
    # 校验失败，在页面上显示错误信息
    return render(request, 'mobile_add.html', {'form': form})

## 新建一个mobileform 使得修改靓号的时候，仅仅可以修改级别、价格和状态

def mobile_edit(request,nid):
    """编辑靓号"""
    row_object = models.MobileInfo.objects.filter(id=nid).first()
    if request.method == "GET":
        form = MobileeditModelForm(instance=row_object)
        return render(request, 'mobile_edit.html', {'form':form})
    form = MobileeditModelForm(data=request.POST, instance=row_object)
    if form.is_valid():
        form.save()
        return redirect("/mobile/list/")
    return render(request, 'mobile_edit.html', {'form': form})

def mobile_delete(request,nid):
    models.MobileInfo.objects.filter(id=nid).delete()
    return redirect("/mobile/list/")

